package com.kvn.test.stepdefs;

import com.kvn.test.core.TestContext;
import cucumber.api.java.en.And;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;

import java.util.Map;

/**
* @author wzy
* @date 2017年10月19日 下午2:28:28
*/
public class AssertsStepdefs {
	private Logger logger = LoggerFactory.getLogger(CommonStepdefs.class);
	
	@And("^ASSERT->DUBBO返回值中存在\\((.*)\\)$")
	public void assertDubboContains(String param){
		Map<String, Object> dubboRlt = TestContext.getInstance().get_D();
		boolean contains = false;
		for(Object value : dubboRlt.values()){
			if(value == null){
				continue;
			}
			if(param.equals(value.toString())){
				contains = true;
				break;
			}
		}
		Assert.assertEquals(contains, true, "DUBBO返回值中不存在:" + param);
		logger.info("【验证成功】DUBBO返回值中存在:" + param);
	}

	@And("^ASSERT->DuBBO返回结果中\\((.*)\\)$")
	public void assertDubboKeyValue(String param){
		String[] kv = param.split("=", 2);
		Assert.assertEquals(kv.length, 2, "参数错误");
		Map<String, Object> dubboRlt = TestContext.getInstance().get_D();
		String acctual = dubboRlt.get(kv[0]) == null ? null : dubboRlt.get(kv[0]).toString();
		Assert.assertEquals(kv[1], acctual, "验证失败：DuBBO返回结果中(" + param + ")");
		logger.info("【验证成功】DuBBO返回结果中(" + param + ")");
	}
}
